################################################################################
# Judicial Ideology as Text -- Table A3 (Static Wordshoal Summary Output)
# Truscott & Romano
# Updated February 2025
################################################################################

################################################################################
# Necessary Packages & Dependencies
################################################################################

source('R/truscott_romano_source.R')

################################################################################
# Retrieve Summary Output from Each Static Wordshoal Run by Weight
################################################################################

{

  wordshoal_static_summaries <- data.frame()

  for (weight in 1:length(wordshoals_static)){

    wordshoal_static_summaries <- bind_rows(wordshoal_static_summaries, wordshoals_static[[weight]]$summary_output %>%
                                              mutate(type = names(wordshoals_static[weight]),
                                                     author = ifelse(author == 'Connor', "O'Connor", author)))

  }

  wordshoal_combined_static <- data.frame()

  for (justice in unique(wordshoal_static_summaries$author)){

    temp_justice_rows <- wordshoal_static_summaries %>%
      filter(author == justice) %>%
      mutate(theta = round(theta, 2),
             se = round(se, 2))
    temp_justice_organized <- data.frame(author = unique(temp_justice_rows$author)[1],
                                         base_theta = temp_justice_rows$theta[temp_justice_rows$type == 'Full'],
                                         Full = paste0(temp_justice_rows$theta[temp_justice_rows$type == 'Full'], ' (', temp_justice_rows$se[temp_justice_rows$type == 'Full'], ')'),
                                         High = paste0(temp_justice_rows$theta[temp_justice_rows$type == 'High'], ' (', temp_justice_rows$se[temp_justice_rows$type == 'High'], ')'),
                                         Low = paste0(temp_justice_rows$theta[temp_justice_rows$type == 'Low'], ' (', temp_justice_rows$se[temp_justice_rows$type == 'Low'], ')'),
                                         Sole = paste0(temp_justice_rows$theta[temp_justice_rows$type == 'Sole'], ' (', temp_justice_rows$se[temp_justice_rows$type == 'Sole'], ')'))

    wordshoal_combined_static <- bind_rows(wordshoal_combined_static, temp_justice_organized)

  }

  wordshoal_static_stargazer <- tibble(
    author = c(wordshoal_combined_static$author),
    Full = c(wordshoal_combined_static$Full),
    High = c(wordshoal_combined_static$High),
    Low = c(wordshoal_combined_static$Low),
    Sole = c(wordshoal_combined_static$Sole)
  )


  wordshoal_combined_static_clean <- wordshoal_static_stargazer %>%
    mutate(across(Full:Sole, ~ gsub(" \\(.*\\)", "", .))) %>%
    mutate(across(Full:Sole, as.numeric)) %>%
    rename(Justice = author)


} # Compile Weight-Level (Static) Wordshoal Estimates as Stargazer

table_A2_text <- stargazer(wordshoal_combined_static_clean, type = "text", summary = FALSE, rownames = FALSE) # As Text

table_A2_latex <- stargazer(wordshoal_combined_static_clean, type = "latex", summary = FALSE, rownames = FALSE, label = 'tab:table_A1', title = 'Static Wordshoal Summary Outputs by Justice (2005-2021 Terms)', style = 'apsr', notes = 'For information regarding associated weighting schemes, please see the Supplemental Appendix.') # As LaTex


writeLines(table_A2_text,
           'tables/Table_A2_text.txt',
           sep = '\n')


writeLines(table_A2_latex,
           'tables/Table_A2_latex.txt',
           sep = '\n')
